<template>
  <div class="app-container w-full h-full overflow-hidden">
    <!--工具栏-->
    <div
      class="head-container head-container overflow-hidden h-full w-full flex flex-col"
    >
      <div class="flex items-center flex-end justify-end search pb-10">
        <el-select
          v-model="query.paramType"
          placeholder="请选择模板类型"
          class="mr-10"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in [
              { label: '成人', value: 1 },
              { label: '儿童', value: 2 },
            ]"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-button
          size="small"
          type="primary"
          class="mr-10"
          @click="handleExport"
        >导入模板</el-button>
        <crudOperation :permission="permission" />
      </div>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%"
        height="100%"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="paramName" label="报告名称" />
        <el-table-column prop="paramType" label="报告类型">
          <template slot-scope="{ row }">
            {{ row.paramType * 1 === 1 ? '成人' : '儿童' }}
          </template>
        </el-table-column>
        <el-table-column prop="teZhiDesc" label="特质综述" />
        <el-table-column prop="jiLiJianYi" label="激励建议" />
        <el-table-column prop="zhuMingRen" label="著名人物" />
        <el-table-column prop="faZhanJianYi" label="发展建议" />
        <el-table-column prop="fengGeNengLi" label="显著风格以及能力" />
        <el-table-column prop="yaLi" label="压力来源" />
        <el-table-column prop="heLiJianYi" label="针对您首要特质的合理化建议" />
        <!-- <el-table-column
          v-if="checkPer(['admin', 'examReportParams:edit', 'examReportParams:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column> -->
      </el-table>
      <!--分页组件-->
      <pagination />
      <el-dialog
        :close-on-click-modal="false"
        :visible.sync="showDialog"
        title="导入模板"
        width="500px"
        @close="showDialog = false"
      >
        <el-form
          ref="uploadForm"
          :model="uploadForm"
          :rules="rules"
          size="small"
          label-width="80px"
        >
          <el-form-item
            label="模板类型"
            prop="paramType"
            :rules="[{ required: true, message: '请选择模板类型', trigger: 'blur' }]"
          >
            <el-select
              v-model="uploadForm.paramType"
              placeholder="请选择模板类型"
              class="w-full"
            >
              <el-option
                v-for="item in [
                  { label: '成人', value: 1 },
                  { label: '儿童', value: 2 },
                ]"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="模板文件"
            prop="file"
            :rules="[{ required: true, message: '请上传模板文件', trigger: 'blur' }]"
          >
            <el-upload
              ref="uploadFile"
              action="/api/examReportParams/upload"
              :limit="1"
              :auto-upload="false"
              accept=".xlsx,.xls"
              :on-remove="handleRemove"
              :on-change="uploadAction"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传excel文件</div>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="default" @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="submitAction">确认</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import crudExamReportParams from '@/api/examReportParams'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import { upload } from '@/utils/upload'
const defaultForm = {
  paramName: null,
  teZhiDesc: null,
  jiLiJianYi: null,
  zhuMingRen: null,
  faZhanJianYi: null,
  paramType: null,
  paramId: null,
  fengGeNengLi: null,
  yaLi: null,
  heLiJianYi: null
}
export default {
  name: 'ExamReportParams',
  components: { pagination, crudOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '测评报告模版数据',
      url: 'api/examReportParams',
      idField: 'paramId',
      sort: 'paramId,desc',
      crudMethod: { ...crudExamReportParams },
      optShow: {
        add: false,
        del: false,
        reset: false,
        upload: false
      }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'examReportParams:add'],
        edit: ['admin', 'examReportParams:edit'],
        del: ['admin', 'examReportParams:del']
      },
      rules: {
        paramName: [{ required: true, message: '报告名称不能为空', trigger: 'blur' }],
        paramId: [{ required: true, message: '主键不能为空', trigger: 'blur' }]
      },
      showDialog: false,
      uploadForm: {
        paramType: '',
        file: null
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    handleExport() {
      this.showDialog = true
    },
    uploadAction(file) {
      this.uploadForm.file = file
    },
    handleRemove() {
      this.$refs['uploadFile'].clearFiles()
    },
    submitAction() {
      this.$refs['uploadForm'].validate((validate) => {
        if (validate) {
          upload(
            '/api/examReportParams/upload?paramType=' + this.uploadForm.paramType,
            this.uploadForm.file.raw
          )
            .then((result) => {
              this.$refs['uploadFile'].clearFiles()
              this.$refs['uploadForm'].resetFields()
              this.showDialog = false
              this.crud.toQuery()
            })
            .catch((err) => {
              console.log(err)
            })
        }
      })
    }
  }
}
</script>

<style scoped></style>
